/*
 * 
 * monoliner
 * 
 * The MIT License
 * 
 * Copyright (c) 2009 101000code/101000LAB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 */
package monoliner;

/**
 *
 * @author  101000code
 */
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.awt.image.DataBufferInt;
import javax.imageio.ImageIO;
import java.awt.Toolkit;
import javax.swing.JFileChooser;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.ImageIcon;
import java.lang.Math.*;
import java.text.DecimalFormat;

public class LinerMain extends javax.swing.JFrame {

    //フロント描画バッファ
    boolean[][] fBuffer;

    //バック描画バッファ(計算対象)
    boolean[][] bBuffer;
    //計算バッファ
    boolean[][] cBuffer;
    //イメージバッファ
    boolean[][][] iBuffer;
    //描画計算パラメタ
    int[] calcParam = {1, 1, 1, 1, 1};
    //白黒フラグ
    boolean monoFlag = true;
    //描画スレッドフラグ
    boolean runDraw = true;
    //計算スレッドフラグ
    boolean runCalc = true;
    //連続描画フラグ
    boolean sequenceFlag = false;
    //ベクトル場フラグ
    boolean vectorFlag = false;
    //十字移動フラグ
    boolean moveTFlag = false;//上
    boolean moveBFlag = false;//下
    boolean moveLFlag = false;//左
    boolean moveRFlag = false;//右

    //マウスカーソル機能フラグ
    /*
     * 0:フロントライン
     * 1:座標変換
     * 2:フロントドットスクウェア
     * 3:バックライン
     * 4:バックドットスクウェア
     * 5:消しゴム
     * 6:バック消しゴム
     * 7:フロントペイント
     * 8:バックペイント
     */
    int cursorFrag = 0;
    //デフォルトスクリーン
    GraphicsDevice defaultScreen;
    //デフォルトスクリーン描画領域サイズ
    int width;
    int height;
    //マルチスクリーン描画領域サイズ
    int secondWidth;
    int secondHeight;
    //座標
    double cx;
    double cy;
    //計算スピード
    int calcSpeed;
    //スクリーン群
    GraphicsDevice[] gs;
    //マルチスクリーン開始フラグ
    boolean multiScreen_flag = false;
    //マルチスクリーン完了フラグ
    boolean multiScreened_flag = false;
    //セカンドスクリーン
    JFrame secondFrame;
    //デフォルトスクリーンコンポーネント
    GraphicComponent display;
    //セカンドスクリーンコンポーネント
    GraphicComponent secondDisplay;
    private Image dbImage = null;
    private Graphics dbg;
    Graphics g;
    Graphics sg;

    // FPS計算用
    private static final int FPS = 30; // 期待するFPS
    private static final long PERIOD = (long) (1.0 / FPS * 1000000000); // 1フレームの時間
    // 単位:
    // ns
    private static long MAX_STATS_INTERVAL = 1000000000L; // FPS計算間隔 単位: ns
    private long calcInterval = 0L;
    private long prevCalcTime;
    private long frameCount = 0; // フレームカウンター
    private double actualFPS = 0.0; // 実際のFPS
    private DecimalFormat df = new DecimalFormat("0.0");

    class GraphicComponent extends JComponent {
    }

    /**
     * バッファ情報を元にイメージを描画
     *
     */
    private void Render() {
        // 初回の呼び出し時にダブルバッファリング用オブジェクトを作成
        if (dbImage == null) {
            // バッファイメージ
            dbImage = createImage(this.width, this.height);
            if (dbImage == null) {
                System.out.println("dbImage is null");
                return;
            } else {
                // バッファイメージの描画オブジェクト
                dbg = dbImage.getGraphics();
            }
        }

        if (monoFlag) {
            // 背景
            dbg.setColor(Color.BLACK);
            dbg.fillRect(0, 0, this.width, this.height);
            // 線
            dbg.setColor(Color.WHITE);
        } else {
            // 背景
            dbg.setColor(Color.WHITE);
            dbg.fillRect(0, 0, this.width, this.height);
            // 線
            dbg.setColor(Color.BLACK);
        }

        // 描画
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                if (fBuffer[i][j] || bBuffer[i][j]) {
                    dbg.drawLine(i, j, i, j);
                //System.out.println(i +":"+ j);
                }
            }
        }

    }

    /**
     * 描画イメージをスクリーンに表示
     *
     */
    private void paintScreen() {
        try {
            g = this.display.getGraphics(); // グラフィックオブジェクトを取得

            if ((g != null) && (dbImage != null)) {
                g.drawImage(this.dbImage, 0, 0, null); // バッファイメージを画面に描画
            }
            if (g != null) {
                g.dispose(); // グラフィックオブジェクトを破棄
            }


            if (multiScreened_flag) {
                sg = this.secondDisplay.getGraphics();
                if ((sg != null) && (dbImage != null) && multiScreened_flag) {
                    sg.drawImage(this.dbImage, 0, 0, this.secondWidth, this.secondHeight, null); // バッファイメージを画面に描画
                //sg.drawImage(this.dbImage, 0, 0, null); // バッファイメージを画面に描画
                }
                if (sg != null) {
                    sg.dispose(); // グラフィックオブジェクトを破棄
                }
            }

            Toolkit.getDefaultToolkit().sync();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("error");
        }
    }

    /**
     * 描画スレッド
     *
     */
    class Drawer implements Runnable {

        public void run() {
            try {
                while (runDraw) {
                    //System.out.println("drawer");
                    //s++;
                    Render();
                    paintScreen();
                    //if(s == 360) s=0;
                    Thread.sleep(1);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("error");
            }
        }
    }

    /**
     * 計算スレッド
     *
     */
    class Calculater implements Runnable {

        public void run() {
            try {
                while (runCalc) {

                    for (int i = 0; i < width; i++) {
                        for (int j = 0; j < height; j++) {

                            if (bBuffer[i][j]) {

                                //点の座標
                                int x = i;
                                int y = j;

                                //次の座標(初期化)
                                int xNext = i;
                                int yNext = j;

                                //ベクトル場の漸化式計算
                                if (vectorFlag) {

                                    //ベクトル場の(0,0)座標を(cx,cy)にずらす
                                    double xr = x - cx;
                                    double yr = y - cy;

                                    //ベクトル場の半径
                                    //中心座標から遠ければ遠いほど速度が遅くなる
                                    double cr = Math.sqrt((xr * xr + yr * yr));

                                    if (cr < calcParam[0]) {

                                        //三角関数の計算
                                        //移動角度
                                        double delta = (Math.PI) / cr;//Math.toRadians(10);

                                        //三角関数sin,cos
                                        double sindelta = Math.sin(calcParam[1] * delta);
                                        double cosdelta = Math.cos(calcParam[2] * delta);

                                        //円の座標を(cx,cy)に戻しつつ次の座標
                                        xNext = (int) (xr * cosdelta - yr * sindelta * calcParam[3] + cx);
                                        yNext = (int) (xr * sindelta * calcParam[4] + yr * cosdelta + cy);

                                    } else {
                                        xNext = x;
                                        yNext = y;
                                    }
                                }

                                /**
                                 * 十字移動の計算
                                 */
                                if (moveTFlag) {
                                    yNext = yNext - 3;
                                }
                                if (moveBFlag) {
                                    yNext = yNext + 3;
                                }
                                if (moveLFlag) {
                                    xNext = xNext - 3;
                                }
                                if (moveRFlag) {
                                    xNext = xNext + 3;
                                }

                                if (0 < xNext && xNext < width && 0 < yNext && yNext < height) {
                                    cBuffer[xNext][yNext] = true;
                                }
                            }
                        }
                    }

                    for (int i = 0; i < width; i++) {
                        for (int j = 0; j < height; j++) {
                            bBuffer[i][j] = cBuffer[i][j];
                            if (!sequenceFlag) {
                                cBuffer[i][j] = false;
                            }
                        }
                    }

                    Thread.sleep(100 - calcSpeed);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("error");
            }


        }
    }

    /**
     * 初期設定コンストラクタ
     *
     */
    public LinerMain() {

        initComponents();

        //ウィンドウアイコンを設定
        ImageIcon icon = new ImageIcon("./src/img/logoicon.png");
        setIconImage(icon.getImage());

        // デフォルトウィンドウの描画領域サイズを設定
        this.width = 640;
        this.height = 480;

        // マルチスクリーンのディスプレイサイズ+描画領域サイズを設定
        this.secondWidth = 640;
        this.secondHeight = 480;

        //初期ボタンセレクト設定
        setCursorBtnSelect(0);

        //初期座標を設定
        this.cx = (int) (this.width / 2);
        this.cy = (int) (this.height / 2);

        //バッファの初期化
        fBuffer = new boolean[this.width][this.height];
        bBuffer = new boolean[this.width][this.height];
        cBuffer = new boolean[this.width][this.height];
        iBuffer = new boolean[this.width][this.height][3];

        // サイズを調整
        this.setBounds(0, 0, this.width + this.getInsets().left + this.getInsets().right, this.height + panelActionTop.getHeight() + this.getInsets().top + this.getInsets().bottom + menuBarMenu.getHeight() + panelActionBottom.getHeight());
        this.display = new GraphicComponent();
        Container cont = getContentPane();
        cont.add(this.display);

        // 各パラメータをスライダーから取得
        this.calcSpeed = this.sldCalcSpeed.getValue();
        this.calcParam[0] = this.sldCalcParam0.getValue();
        this.calcParam[1] = this.sldCalcParam1.getValue();
        this.calcParam[2] = this.sldCalcParam2.getValue();
        this.calcParam[3] = this.sldCalcParam3.getValue();
        this.calcParam[4] = this.sldCalcParam4.getValue();

        //描画スレッドの実行
        Runnable drawer = new Drawer();
        Thread t = new Thread(drawer);
        t.start();


        //計算スレッドの実行
        Runnable calculater = new Calculater();
        Thread c = new Thread(calculater);
        c.start();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelActionTop = new javax.swing.JPanel();
        btnImage0 = new javax.swing.JButton();
        btnSetImage0 = new javax.swing.JButton();
        btnImage1 = new javax.swing.JButton();
        btnSetImage1 = new javax.swing.JButton();
        btnImage2 = new javax.swing.JButton();
        btnSetImage2 = new javax.swing.JButton();
        sldCalcParam0 = new javax.swing.JSlider();
        sldCalcSpeed = new javax.swing.JSlider();
        sldCalcParam1 = new javax.swing.JSlider();
        sldCalcParam2 = new javax.swing.JSlider();
        sldCalcParam3 = new javax.swing.JSlider();
        sldCalcParam4 = new javax.swing.JSlider();
        panelActionBottom = new javax.swing.JPanel();
        btnCalc = new javax.swing.JButton();
        btnMoveRight = new javax.swing.JButton();
        btnMoveBottom = new javax.swing.JButton();
        btnMoveTop = new javax.swing.JButton();
        btnMoveLeft = new javax.swing.JButton();
        btnSequence = new javax.swing.JButton();
        btnCoordinates = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        btnFtoB = new javax.swing.JButton();
        btnChangeMono = new javax.swing.JButton();
        btnLine = new javax.swing.JButton();
        btnLineBack = new javax.swing.JButton();
        btnDotSquare = new javax.swing.JButton();
        btnDotSquareBack = new javax.swing.JButton();
        btnErase = new javax.swing.JButton();
        btnEraseBack = new javax.swing.JButton();
        btnPaint = new javax.swing.JButton();
        btnPaintBack = new javax.swing.JButton();
        menuBarMenu = new javax.swing.JMenuBar();
        menuMenu = new javax.swing.JMenu();
        menuSetting = new javax.swing.JMenu();
        menuItemMulti = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("mono liner");
        setBackground(new java.awt.Color(0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(new java.awt.Color(0, 0, 0));
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });

        panelActionTop.setPreferredSize(new java.awt.Dimension(100, 80));

        btnImage0.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/image.png"))); // NOI18N
        btnImage0.setToolTipText("Image");
        btnImage0.setEnabled(false);
        btnImage0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImage0ActionPerformed(evt);
            }
        });

        btnSetImage0.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/top.png"))); // NOI18N
        btnSetImage0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetImage0ActionPerformed(evt);
            }
        });

        btnImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/image.png"))); // NOI18N
        btnImage1.setEnabled(false);
        btnImage1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImage1ActionPerformed(evt);
            }
        });

        btnSetImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/top.png"))); // NOI18N
        btnSetImage1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetImage1ActionPerformed(evt);
            }
        });

        btnImage2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/image.png"))); // NOI18N
        btnImage2.setEnabled(false);
        btnImage2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImage2ActionPerformed(evt);
            }
        });

        btnSetImage2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/top.png"))); // NOI18N
        btnSetImage2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetImage2ActionPerformed(evt);
            }
        });

        sldCalcParam0.setMaximum(250);
        sldCalcParam0.setMinimum(10);
        sldCalcParam0.setOrientation(javax.swing.JSlider.VERTICAL);
        sldCalcParam0.setValue(100);
        sldCalcParam0.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldCalcParam0StateChanged(evt);
            }
        });

        sldCalcSpeed.setOrientation(javax.swing.JSlider.VERTICAL);
        sldCalcSpeed.setToolTipText("CalcSpeed");
        sldCalcSpeed.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldCalcSpeedStateChanged(evt);
            }
        });

        sldCalcParam1.setMaximum(5);
        sldCalcParam1.setMinimum(1);
        sldCalcParam1.setOrientation(javax.swing.JSlider.VERTICAL);
        sldCalcParam1.setValue(1);
        sldCalcParam1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldCalcParam1StateChanged(evt);
            }
        });

        sldCalcParam2.setMaximum(5);
        sldCalcParam2.setMinimum(1);
        sldCalcParam2.setOrientation(javax.swing.JSlider.VERTICAL);
        sldCalcParam2.setValue(1);
        sldCalcParam2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldCalcParam2StateChanged(evt);
            }
        });

        sldCalcParam3.setMaximum(5);
        sldCalcParam3.setMinimum(1);
        sldCalcParam3.setOrientation(javax.swing.JSlider.VERTICAL);
        sldCalcParam3.setValue(1);
        sldCalcParam3.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldCalcParam3StateChanged(evt);
            }
        });

        sldCalcParam4.setMaximum(5);
        sldCalcParam4.setMinimum(1);
        sldCalcParam4.setOrientation(javax.swing.JSlider.VERTICAL);
        sldCalcParam4.setValue(1);
        sldCalcParam4.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldCalcParam4StateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout panelActionTopLayout = new org.jdesktop.layout.GroupLayout(panelActionTop);
        panelActionTop.setLayout(panelActionTopLayout);
        panelActionTopLayout.setHorizontalGroup(
            panelActionTopLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, panelActionTopLayout.createSequentialGroup()
                .addContainerGap()
                .add(sldCalcSpeed, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(sldCalcParam0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(86, 86, 86)
                .add(sldCalcParam1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(sldCalcParam2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(sldCalcParam3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(sldCalcParam4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 187, Short.MAX_VALUE)
                .add(panelActionTopLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(btnSetImage2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnImage2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panelActionTopLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(btnSetImage1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnImage1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panelActionTopLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(btnSetImage0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnImage0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        panelActionTopLayout.setVerticalGroup(
            panelActionTopLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelActionTopLayout.createSequentialGroup()
                .add(panelActionTopLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelActionTopLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(panelActionTopLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(btnImage0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnImage2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnImage1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(panelActionTopLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(btnSetImage0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnSetImage1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnSetImage2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(sldCalcSpeed, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 80, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(sldCalcParam0, 0, 0, Short.MAX_VALUE)
                    .add(panelActionTopLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, sldCalcParam3, 0, 0, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, sldCalcParam2, 0, 0, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, sldCalcParam1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                    .add(sldCalcParam4, 0, 0, Short.MAX_VALUE))
                .addContainerGap())
        );

        getContentPane().add(panelActionTop, java.awt.BorderLayout.NORTH);

        panelActionBottom.setPreferredSize(new java.awt.Dimension(100, 110));

        btnCalc.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/circle.png"))); // NOI18N
        btnCalc.setPreferredSize(new java.awt.Dimension(65, 55));
        btnCalc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalcActionPerformed(evt);
            }
        });

        btnMoveRight.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/right.png"))); // NOI18N
        btnMoveRight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveRightActionPerformed(evt);
            }
        });

        btnMoveBottom.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/bottom.png"))); // NOI18N
        btnMoveBottom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveBottomActionPerformed(evt);
            }
        });

        btnMoveTop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/top.png"))); // NOI18N
        btnMoveTop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveTopActionPerformed(evt);
            }
        });

        btnMoveLeft.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/left.png"))); // NOI18N
        btnMoveLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveLeftActionPerformed(evt);
            }
        });

        btnSequence.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/sequence.png"))); // NOI18N
        btnSequence.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSequenceActionPerformed(evt);
            }
        });

        btnCoordinates.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/coordinates.png"))); // NOI18N
        btnCoordinates.setMaximumSize(new java.awt.Dimension(59, 21));
        btnCoordinates.setMinimumSize(new java.awt.Dimension(59, 21));
        btnCoordinates.setPreferredSize(new java.awt.Dimension(65, 55));
        btnCoordinates.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCoordinatesActionPerformed(evt);
            }
        });

        btnClear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clear.png"))); // NOI18N
        btnClear.setToolTipText("Clear");
        btnClear.setPreferredSize(new java.awt.Dimension(65, 55));
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        btnFtoB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/front_to_back.png"))); // NOI18N
        btnFtoB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFtoBActionPerformed(evt);
            }
        });

        btnChangeMono.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/mono.png"))); // NOI18N
        btnChangeMono.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeMonoActionPerformed(evt);
            }
        });

        btnLine.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/line.png"))); // NOI18N
        btnLine.setToolTipText("DrawFront");
        btnLine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLineActionPerformed(evt);
            }
        });

        btnLineBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/line_back.png"))); // NOI18N
        btnLineBack.setToolTipText("DrawBack");
        btnLineBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLineBackActionPerformed(evt);
            }
        });

        btnDotSquare.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/dot.png"))); // NOI18N
        btnDotSquare.setToolTipText("DotSquare");
        btnDotSquare.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDotSquareActionPerformed(evt);
            }
        });

        btnDotSquareBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/dot_back.png"))); // NOI18N
        btnDotSquareBack.setToolTipText("DotSquareBack");
        btnDotSquareBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDotSquareBackActionPerformed(evt);
            }
        });

        btnErase.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/erase.png"))); // NOI18N
        btnErase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEraseActionPerformed(evt);
            }
        });

        btnEraseBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/erace_back.png"))); // NOI18N
        btnEraseBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEraseBackActionPerformed(evt);
            }
        });

        btnPaint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/paint.png"))); // NOI18N
        btnPaint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPaintActionPerformed(evt);
            }
        });

        btnPaintBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/paint_back.png"))); // NOI18N
        btnPaintBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPaintBackActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout panelActionBottomLayout = new org.jdesktop.layout.GroupLayout(panelActionBottom);
        panelActionBottom.setLayout(panelActionBottomLayout);
        panelActionBottomLayout.setHorizontalGroup(
            panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, panelActionBottomLayout.createSequentialGroup()
                .addContainerGap()
                .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(btnLine, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnLineBack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(btnDotSquare, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnDotSquareBack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(btnErase, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnEraseBack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelActionBottomLayout.createSequentialGroup()
                        .add(btnPaint, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(38, 38, 38)
                        .add(btnCalc, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnCoordinates, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnFtoB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnSequence, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnClear, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnChangeMono, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(btnPaintBack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnMoveLeft, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(btnMoveTop, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnMoveBottom, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnMoveRight, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        panelActionBottomLayout.setVerticalGroup(
            panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelActionBottomLayout.createSequentialGroup()
                .addContainerGap()
                .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelActionBottomLayout.createSequentialGroup()
                        .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(btnLine, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnDotSquare, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnErase, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnPaint, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(btnLineBack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(btnDotSquareBack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(btnEraseBack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(btnPaintBack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(btnSequence, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnCoordinates, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnFtoB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnChangeMono, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnCalc, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnClear, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(btnMoveLeft, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(panelActionBottomLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, panelActionBottomLayout.createSequentialGroup()
                            .add(btnMoveTop, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(btnMoveBottom, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, btnMoveRight, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        getContentPane().add(panelActionBottom, java.awt.BorderLayout.PAGE_END);

        menuMenu.setText("Menu");
        menuBarMenu.add(menuMenu);

        menuSetting.setText("Setting");

        menuItemMulti.setText("MultiScreen");
        menuItemMulti.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemMultiActionPerformed(evt);
            }
        });
        menuSetting.add(menuItemMulti);

        menuBarMenu.add(menuSetting);

        setJMenuBar(menuBarMenu);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void sldCalcSpeedStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldCalcSpeedStateChanged
        this.calcSpeed = this.sldCalcSpeed.getValue();
    }//GEN-LAST:event_sldCalcSpeedStateChanged

    private void btnEraseBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEraseBackActionPerformed
        this.cursorFrag = 6;
        setCursorBtnSelect(6);
    }//GEN-LAST:event_btnEraseBackActionPerformed

    private void btnEraseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEraseActionPerformed
        this.cursorFrag = 5;
        setCursorBtnSelect(5);
    }//GEN-LAST:event_btnEraseActionPerformed

    private void btnChangeMonoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeMonoActionPerformed

        this.monoFlag = !this.monoFlag;
    }//GEN-LAST:event_btnChangeMonoActionPerformed

    private void btnSetImage2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetImage2ActionPerformed

        //JFileChooserオブジェクト生成
        JFileChooser chooser = new JFileChooser(".");
        ImageFilter filter = new ImageFilter();
        chooser.setFileFilter(filter);

        //「ファイルを開く」ダイアログを表示
        if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
            return;
        }

        File fChoosen = chooser.getSelectedFile();
        try {
            FileInputStream inImage = new FileInputStream(fChoosen);
            //rtfEditor.read(in, doc, 0);
            BufferedImage image = ImageIO.read(inImage);//読み込んだイメージ
            BufferedImage cpyImage = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);//コピー用イメージ
            Graphics2D g2d = cpyImage.createGraphics();
            g2d.drawImage(image, 0, 0, this.width, this.height, null);//読み込んだイメージをコピー
            inImage.close();


            WritableRaster ras = cpyImage.getRaster();
            DataBufferInt buffer = (DataBufferInt) (ras.getDataBuffer());

            //pixel情報取得/イメージバッファに格納
            for (int i = 0; i < this.width; i++) {
                for (int j = 0; j < this.height; j++) {
                    if (buffer.getElem(i + j * this.width) == 0) {
                        iBuffer[i][j][2] = true;
                    //System.out.println(buffer.getElem(i+j*this.width));
                    } else {
                        iBuffer[i][j][2] = false;
                    }

                }
            }

            //イメージバッファボタンを有効化
            this.btnImage2.setEnabled(true);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnSetImage2ActionPerformed

    private void btnImage2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImage2ActionPerformed

        for (int i = 0; i < this.width; i++) {
            for (int j = 0; j < this.height; j++) {
                if (iBuffer[i][j][2]) {
                    fBuffer[i][j] = true;
                }
            }
        }
    }//GEN-LAST:event_btnImage2ActionPerformed

    private void btnSetImage1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetImage1ActionPerformed

        //JFileChooserオブジェクト生成
        JFileChooser chooser = new JFileChooser(".");
        ImageFilter filter = new ImageFilter();
        chooser.setFileFilter(filter);

        //「ファイルを開く」ダイアログを表示
        if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
            return;
        }

        File fChoosen = chooser.getSelectedFile();
        try {
            FileInputStream inImage = new FileInputStream(fChoosen);
            //rtfEditor.read(in, doc, 0);
            BufferedImage image = ImageIO.read(inImage);//読み込んだイメージ
            BufferedImage cpyImage = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);//コピー用イメージ
            Graphics2D g2d = cpyImage.createGraphics();
            g2d.drawImage(image, 0, 0, this.width, this.height, null);//読み込んだイメージをコピー
            inImage.close();


            WritableRaster ras = cpyImage.getRaster();
            DataBufferInt buffer = (DataBufferInt) (ras.getDataBuffer());

            //pixel情報取得/イメージバッファに格納
            for (int i = 0; i < this.width; i++) {
                for (int j = 0; j < this.height; j++) {
                    if (buffer.getElem(i + j * this.width) == 0) {
                        iBuffer[i][j][1] = true;
                    //System.out.println(buffer.getElem(i+j*this.width));
                    } else {
                        iBuffer[i][j][1] = false;
                    }

                }
            }

            //イメージバッファボタンを有効化
            this.btnImage1.setEnabled(true);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
    }//GEN-LAST:event_btnSetImage1ActionPerformed

    private void btnImage1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImage1ActionPerformed
        for (int i = 0; i < this.width; i++) {
            for (int j = 0; j < this.height; j++) {
                if (iBuffer[i][j][1]) {
                    fBuffer[i][j] = true;
                }
            }
        }
    }//GEN-LAST:event_btnImage1ActionPerformed

    //イメージバッファ0にイメージセット
    private void btnSetImage0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetImage0ActionPerformed
//JFileChooserオブジェクト生成
        JFileChooser chooser = new JFileChooser(".");
        ImageFilter filter = new ImageFilter();
        chooser.setFileFilter(filter);

        //「ファイルを開く」ダイアログを表示
        if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
            return;
        }

        File fChoosen = chooser.getSelectedFile();
        try {
            FileInputStream inImage = new FileInputStream(fChoosen);
            //rtfEditor.read(in, doc, 0);
            BufferedImage image = ImageIO.read(inImage);//読み込んだイメージ
            BufferedImage cpyImage = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);//コピー用イメージ
            Graphics2D g2d = cpyImage.createGraphics();
            g2d.drawImage(image, 0, 0, this.width, this.height, null);//読み込んだイメージをコピー
            inImage.close();


            WritableRaster ras = cpyImage.getRaster();
            DataBufferInt buffer = (DataBufferInt) (ras.getDataBuffer());

            //pixel情報取得/イメージバッファに格納
            for (int i = 0; i < this.width; i++) {
                for (int j = 0; j < this.height; j++) {
                    if (buffer.getElem(i + j * this.width) == 0) {
                        iBuffer[i][j][0] = true;
                    //System.out.println(buffer.getElem(i+j*this.width));
                    } else {
                        iBuffer[i][j][0] = false;
                    }

                }
            }

            //イメージバッファボタンを有効化
            this.btnImage0.setEnabled(true);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
    }//GEN-LAST:event_btnSetImage0ActionPerformed

    private void btnMoveLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveLeftActionPerformed
        this.moveLFlag = !this.moveLFlag;
        if (this.moveLFlag) {
            this.btnMoveLeft.setBackground(Color.white);
            this.moveRFlag = false;
            this.btnMoveRight.setBackground(null);
        } else {
            this.btnMoveLeft.setBackground(null);
        }
    }//GEN-LAST:event_btnMoveLeftActionPerformed

    private void btnMoveRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveRightActionPerformed
        this.moveRFlag = !this.moveRFlag;
        if (this.moveRFlag) {
            this.btnMoveRight.setBackground(Color.white);
            this.moveLFlag = false;
            this.btnMoveLeft.setBackground(null);
        } else {
            this.btnMoveRight.setBackground(null);
        }
    }//GEN-LAST:event_btnMoveRightActionPerformed

    private void btnMoveBottomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveBottomActionPerformed
        this.moveBFlag = !this.moveBFlag;
        if (this.moveBFlag) {
            this.btnMoveBottom.setBackground(Color.white);
            this.moveTFlag = false;
            this.btnMoveTop.setBackground(null);
        } else {
            this.btnMoveBottom.setBackground(null);
        }
    }//GEN-LAST:event_btnMoveBottomActionPerformed

    private void btnMoveTopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveTopActionPerformed
        this.moveTFlag = !this.moveTFlag;
        if (this.moveTFlag) {
            this.btnMoveTop.setBackground(Color.white);
            this.moveBFlag = false;
            this.btnMoveBottom.setBackground(null);
        } else {
            this.btnMoveTop.setBackground(null);
        }
    }//GEN-LAST:event_btnMoveTopActionPerformed

    //イメージ描画
    private void btnImage0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImage0ActionPerformed
        for (int i = 0; i < this.width; i++) {
            for (int j = 0; j < this.height; j++) {
                if (iBuffer[i][j][0]) {
                    fBuffer[i][j] = true;
                }
            }
        }
    }//GEN-LAST:event_btnImage0ActionPerformed

    //バックドットスクエア
    private void btnDotSquareBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDotSquareBackActionPerformed
        this.cursorFrag = 4;
        setCursorBtnSelect(4);
    }//GEN-LAST:event_btnDotSquareBackActionPerformed

    //バックライン描画
    private void btnLineBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLineBackActionPerformed
        this.cursorFrag = 3;
        setCursorBtnSelect(3);
    }//GEN-LAST:event_btnLineBackActionPerformed

    //フロントバッファ情報をバックバッファ情報にうつす
    private void btnFtoBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFtoBActionPerformed
        for (int i = 0; i < this.width; i++) {
            for (int j = 0; j < this.height; j++) {
                if (fBuffer[i][j]) {
                    bBuffer[i][j] = true;
                    fBuffer[i][j] = false;
                }
            }
        }
    }//GEN-LAST:event_btnFtoBActionPerformed

    //イメージファイル設置
    //連続描画
    private void btnSequenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSequenceActionPerformed
        this.sequenceFlag = !this.sequenceFlag;
        if (this.sequenceFlag) {
            this.btnSequence.setBackground(Color.white);
        } else {
            this.btnSequence.setBackground(null);
        }
    }//GEN-LAST:event_btnSequenceActionPerformed

    //ドットスクウェアボタン
    private void btnDotSquareActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDotSquareActionPerformed
        this.cursorFrag = 2;
        setCursorBtnSelect(2);
    }//GEN-LAST:event_btnDotSquareActionPerformed

    //ライン描画ボタン
    private void btnLineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLineActionPerformed
        this.cursorFrag = 0;
        setCursorBtnSelect(0);
    }//GEN-LAST:event_btnLineActionPerformed

    //座標変更ボタン
    private void btnCoordinatesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCoordinatesActionPerformed
        this.cursorFrag = 1;
        setCursorBtnSelect(1);
    }//GEN-LAST:event_btnCoordinatesActionPerformed

    //マルチディスプレイ
    private void menuItemMultiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemMultiActionPerformed
        this.multiScreen_flag = true;
        setMultiDisplay();
    }//GEN-LAST:event_menuItemMultiActionPerformed

    //描画情報クリアボタン
    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        for (int i = 0; i < this.width; i++) {
            for (int j = 0; j < this.height; j++) {
                bBuffer[i][j] = false;
                cBuffer[i][j] = false;
            }
        }
    }//GEN-LAST:event_btnClearActionPerformed

    //ベクトル場開始ボタン
    private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCalcActionPerformed

        if (!vectorFlag) {
            this.btnCalc.setBackground(Color.white);
            vectorFlag = true;
        } else {
            this.btnCalc.setBackground(null);
            vectorFlag = false;
        }
    }//GEN-LAST:event_btnCalcActionPerformed

    //マウスプレスメソッド
    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed

        int drawX = evt.getX() - this.getInsets().left;
        int drawY = evt.getY() - this.getInsets().top - menuBarMenu.getHeight() - panelActionTop.getHeight();
        if (drawX >= 0 && drawX < this.width && drawY >= 0 && drawY < this.height) {
            switch (cursorFrag) {
                case 0://ライン描画
                    fBuffer[drawX][drawY] = true;
                    break;

                case 1://座標変換
                    this.cx = drawX;
                    this.cy = drawY;
                    break;

                case 2://ドットスクエア
                    for (int i = drawX - 15; i < drawX + 15; i = i + 3) {
                        for (int j = drawY - 15; j < drawY + 15; j = j + 3) {
                            if (i >= 0 && i < this.width && j >= 0 && j < this.height) {
                                fBuffer[i][j] = true;
                            }
                        }
                    }
                    break;

                case 3://バックライン
                    bBuffer[drawX][drawY] = true;
                    break;

                case 4://バックドットスクエア
                    for (int i = drawX - 15; i < drawX + 15; i = i + 3) {
                        for (int j = drawY - 15; j < drawY + 15; j = j + 3) {
                            if (i >= 0 && i < this.width && j >= 0 && j < this.height) {
                                bBuffer[i][j] = true;
                            }
                        }
                    }
                    break;

                case 5://消しゴム
                    for (int i = drawX - 10; i < drawX + 10; i++) {
                        for (int j = drawY - 10; j < drawY + 10; j++) {
                            if (Math.abs(drawX - i) + Math.abs(drawY - j) < 10) {
                                if (i >= 0 && i < this.width && j >= 0 && j < this.height) {
                                    fBuffer[i][j] = false;
                                }
                            }
                        }
                    }
                    break;

                case 6://消しゴムバック
                    for (int i = drawX - 10; i < drawX + 10; i++) {
                        for (int j = drawY - 10; j < drawY + 10; j++) {
                            if (Math.abs(drawX - i) + Math.abs(drawY - j) < 10) {
                                if (i >= 0 && i < this.width && j >= 0 && j < this.height) {
                                    bBuffer[i][j] = false;
                                }
                            }
                        }
                    }
                    break;

                case 7://フロントペイント
                    int j = drawY + 1;
                    while (j < this.height) {
                        if (fBuffer[drawX][j] == false) {
                            fBuffer[drawX][j] = true;
                            j++;
                        } else {
                            break;
                        }
                    }
                    j = drawY;
                    while (j > 0) {
                        if (fBuffer[drawX][j] == false) {
                            fBuffer[drawX][j] = true;
                            j--;
                        } else {
                            break;
                        }
                    }
                    break;

                case 8://バックペイント
                    int k = drawY + 1;
                    while (k < this.height) {
                        if (bBuffer[drawX][k] == false) {
                            bBuffer[drawX][k] = true;
                            k++;
                        } else {
                            break;
                        }
                    }
                    k = drawY;
                    while (k > 0) {
                        if (bBuffer[drawX][k] == false) {
                            bBuffer[drawX][k] = true;
                            k--;
                        } else {
                            break;
                        }
                    }
                    break;

                default:
                    fBuffer[drawX][drawY] = true;
                    break;
            }
        }
        
    }//GEN-LAST:event_formMousePressed

    //マウスドラッグメソッド
    private void formMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseDragged

        int drawX = evt.getX() - this.getInsets().left;
        int drawY = evt.getY() - this.getInsets().top - menuBarMenu.getHeight() - panelActionTop.getHeight();
        if (drawX >= 0 && drawX < this.width && drawY >= 0 && drawY < this.height) {
            switch (cursorFrag) {
                case 0:
                    fBuffer[drawX][drawY] = true;
                    break;

                case 1:
                    this.cx = drawX;
                    this.cy = drawY;
                    break;

                case 2:
                    for (int i = drawX - 15; i < drawX + 15; i = i + 3) {
                        for (int j = drawY - 15; j < drawY + 15; j = j + 3) {
                            if (i >= 0 && i < this.width && j >= 0 && j < this.height) {
                                fBuffer[i][j] = true;
                            }
                        }
                    }
                    break;

                case 3://バックライン
                    bBuffer[drawX][drawY] = true;
                    break;

                case 4://バックドットスクエア
                    for (int i = drawX - 15; i < drawX + 15; i = i + 3) {
                        for (int j = drawY - 15; j < drawY + 15; j = j + 3) {
                            if (i >= 0 && i < this.width && j >= 0 && j < this.height) {
                                bBuffer[i][j] = true;
                            }
                        }
                    }
                    break;

                case 5://消しゴム
                    for (int i = drawX - 10; i < drawX + 10; i++) {
                        for (int j = drawY - 10; j < drawY + 10; j++) {
                            if (Math.abs(drawX - i) + Math.abs(drawY - j) < 10) {
                                if (i >= 0 && i < this.width && j >= 0 && j < this.height) {
                                    fBuffer[i][j] = false;
                                }
                            }
                        }
                    }
                    break;

                case 6://消しゴムバック
                    for (int i = drawX - 10; i < drawX + 10; i++) {
                        for (int j = drawY - 10; j < drawY + 10; j++) {
                            if (Math.abs(drawX - i) + Math.abs(drawY - j) < 10) {
                                if (i >= 0 && i < this.width && j >= 0 && j < this.height) {
                                    bBuffer[i][j] = false;
                                }
                            }
                        }
                    }
                    break;

                case 7://フロントペイント
                    int j = drawY + 1;
                    while (j < this.height) {
                        if (fBuffer[drawX][j] == false) {
                            fBuffer[drawX][j] = true;
                            j++;
                        } else {
                            break;
                        }
                    }
                    j = drawY;
                    while (j > 0) {
                        if (fBuffer[drawX][j] == false) {
                            fBuffer[drawX][j] = true;
                            j--;
                        } else {
                            break;
                        }
                    }
                    break;

                case 8://バックペイント
                    int k = drawY + 1;
                    while (k < this.height) {
                        if (bBuffer[drawX][k] == false) {
                            bBuffer[drawX][k] = true;
                            k++;
                        } else {
                            break;
                        }
                    }
                    k = drawY;
                    while (k > 0) {
                        if (bBuffer[drawX][k] == false) {
                            bBuffer[drawX][k] = true;
                            k--;
                        } else {
                            break;
                        }
                    }
                    break;

                default:
                    bBuffer[drawX][drawY] = true;
                    break;
            }
        }
        
    }//GEN-LAST:event_formMouseDragged

    /**
     * 描画計算パラメータ
     * @param evt
     */
    private void sldCalcParam0StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldCalcParam0StateChanged
        this.calcParam[0] = this.sldCalcParam0.getValue();
    }//GEN-LAST:event_sldCalcParam0StateChanged

    private void sldCalcParam1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldCalcParam1StateChanged
        this.calcParam[1] = this.sldCalcParam1.getValue();
    }//GEN-LAST:event_sldCalcParam1StateChanged

    private void sldCalcParam2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldCalcParam2StateChanged
        this.calcParam[2] = this.sldCalcParam2.getValue();
    }//GEN-LAST:event_sldCalcParam2StateChanged

    private void sldCalcParam3StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldCalcParam3StateChanged
        this.calcParam[3] = this.sldCalcParam3.getValue();
    }//GEN-LAST:event_sldCalcParam3StateChanged

    private void sldCalcParam4StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldCalcParam4StateChanged
        this.calcParam[4] = this.sldCalcParam4.getValue();
    }//GEN-LAST:event_sldCalcParam4StateChanged

    //フロントペイント
    private void btnPaintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPaintActionPerformed
        this.cursorFrag = 7;
        setCursorBtnSelect(7);
    }//GEN-LAST:event_btnPaintActionPerformed

    private void btnPaintBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPaintBackActionPerformed
        this.cursorFrag = 8;
        setCursorBtnSelect(8);
    }//GEN-LAST:event_btnPaintBackActionPerformed

    /**
     * カーソル関係ボタン状態設定
     *
     */
    private void setCursorBtnSelect(int id) {
        //System.out.println(id);
        //ボタン状態初期化
        this.btnLine.setBackground(null);
        this.btnCoordinates.setBackground(null);
        this.btnDotSquare.setBackground(null);
        this.btnLineBack.setBackground(null);
        this.btnDotSquareBack.setBackground(null);
        this.btnErase.setBackground(null);
        this.btnEraseBack.setBackground(null);
        this.btnPaint.setBackground(null);

        switch (id) {
            case 0:
                this.btnLine.setBackground(Color.white);
                break;

            case 1:
                this.btnCoordinates.setBackground(Color.white);
                break;

            case 2:
                this.btnDotSquare.setBackground(Color.white);
                break;

            case 3:
                this.btnLineBack.setBackground(Color.white);
                break;

            case 4:
                this.btnDotSquareBack.setBackground(Color.white);
                break;

            case 5:
                this.btnErase.setBackground(Color.white);
                break;

            case 6:
                this.btnEraseBack.setBackground(Color.white);
                break;

            case 7:
                this.btnPaint.setBackground(Color.white);
                break;

            default:
                break;
        }
    }

    /**
     * マルチディスプレイ設定
     *
     */
    private void setMultiDisplay() {

        this.runDraw = false;

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        //GraphicsDevicesを取得
        gs = ge.getScreenDevices();
        //デフォルトのGraphicsDeviceを取得
        defaultScreen = ge.getDefaultScreenDevice();

        //GraphicsDeviceの数を表示
        javax.swing.JOptionPane.showMessageDialog(this, gs.length);

        for (int i = 0; i < gs.length; i++) {
            GraphicsDevice gd = gs[i];
            //デフォルトスクリーン以外にJFrameを表示
            if (gd != defaultScreen) {
                //GraphicsConfiguration gc = gd.getDefaultConfiguration();

                secondFrame = new JFrame(gs[i].getDefaultConfiguration());

                showDisplayModes();

                //FullScreen
                if (!gs[i].isFullScreenSupported()) {
                    javax.swing.JOptionPane.showMessageDialog(this, "フルスクリーンモードはサポートされていません。");
                    return;
                } else {
                    secondFrame.setUndecorated(true); // タイトルバー・ボーダー非表示
                    gs[i].setFullScreenWindow(this.secondFrame);
                }

                secondFrame.setBounds(0, 0, secondFrame.getBounds().width, secondFrame.getBounds().height);
                //secondFrame.setBounds(0,0,secondWidth,secondHeight);

                DisplayMode dm = new DisplayMode(secondWidth, secondHeight, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
                //DisplayMode dm = new DisplayMode(width, height, 32,DisplayMode.REFRESH_RATE_UNKNOWN);
                gs[i].setDisplayMode(dm);

                this.secondDisplay = new GraphicComponent();
                secondFrame.add(this.secondDisplay);
                secondFrame.setVisible(true);
                this.multiScreened_flag = true;
                this.runDraw = true;
                System.out.println("setMulti");
            }
        }

        //描画スレッドの再実行
        Runnable drawer = new Drawer();
        Thread t = new Thread(drawer);
        t.start();

        this.runDraw = true;
    }

    /**
     * 変更可能なディスプレイモードをSystem.outに表示
     *
     */
    private void showDisplayModes() {
        System.out.println("変更可能なディスプレイモード");
        for (int j = 0; j < gs.length; j++) {
            DisplayMode[] modes = gs[j].getDisplayModes();
            for (int i = 0; i < modes.length; i++) {
                System.out.print("(" + modes[i].getWidth() + "," + modes[i].getHeight() + "," + modes[i].getBitDepth() + "," + modes[i].getRefreshRate() + ") ");
                if ((i + 1) % 4 == 0) {
                    System.out.println();
                }
            }
            System.out.println();
        }
    }

    /**
     * FPSの計算
     *
     */
    private void calcBufferPS() {
        frameCount++;
        calcInterval += PERIOD;

        // 1秒おきにFPSを再計算する
        if (calcInterval >= MAX_STATS_INTERVAL) {
            long timeNow = System.nanoTime();
            // 実際の経過時間を測定
            long realElapsedTime = timeNow - prevCalcTime; // 単位: ns

            // FPSを計算
            // realElapsedTimeの単位はnsなのでsに変換する
            actualFPS = ((double) frameCount / realElapsedTime) * 1000000000L;
            // System.out.println(df.format(actualFPS));

            frameCount = 0L;
            calcInterval = 0L;
            prevCalcTime = timeNow;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LinerMain().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCalc;
    private javax.swing.JButton btnChangeMono;
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnCoordinates;
    private javax.swing.JButton btnDotSquare;
    private javax.swing.JButton btnDotSquareBack;
    private javax.swing.JButton btnErase;
    private javax.swing.JButton btnEraseBack;
    private javax.swing.JButton btnFtoB;
    private javax.swing.JButton btnImage0;
    private javax.swing.JButton btnImage1;
    private javax.swing.JButton btnImage2;
    private javax.swing.JButton btnLine;
    private javax.swing.JButton btnLineBack;
    private javax.swing.JButton btnMoveBottom;
    private javax.swing.JButton btnMoveLeft;
    private javax.swing.JButton btnMoveRight;
    private javax.swing.JButton btnMoveTop;
    private javax.swing.JButton btnPaint;
    private javax.swing.JButton btnPaintBack;
    private javax.swing.JButton btnSequence;
    private javax.swing.JButton btnSetImage0;
    private javax.swing.JButton btnSetImage1;
    private javax.swing.JButton btnSetImage2;
    private javax.swing.JMenuBar menuBarMenu;
    private javax.swing.JMenuItem menuItemMulti;
    private javax.swing.JMenu menuMenu;
    private javax.swing.JMenu menuSetting;
    private javax.swing.JPanel panelActionBottom;
    private javax.swing.JPanel panelActionTop;
    private javax.swing.JSlider sldCalcParam0;
    private javax.swing.JSlider sldCalcParam1;
    private javax.swing.JSlider sldCalcParam2;
    private javax.swing.JSlider sldCalcParam3;
    private javax.swing.JSlider sldCalcParam4;
    private javax.swing.JSlider sldCalcSpeed;
    // End of variables declaration//GEN-END:variables
}
